package com.sell.demo;

import java.util.Properties;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import com.github.pagehelper.PageHelper;

@SpringBootApplication
@EnableSwagger2
@MapperScan(basePackages = "com.sell.demo.mapper")
public class VueSell2Application {
	
	/** 
     * 跨域过滤器 
     * @return 
     */  
//    @Bean  
//    public CorsFilter corsFilter() {   
//    	System.out.println("跨域");
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();  
//        source.registerCorsConfiguration("/**", buildConfig()); // 4  
//        return new CorsFilter(source);  
//    } 
//    
//    private CorsConfiguration buildConfig() {  
//        CorsConfiguration corsConfiguration = new CorsConfiguration();  
//        corsConfiguration.addAllowedOrigin("http://localhost:8080");  
//        corsConfiguration.addAllowedHeader("*");  
//        corsConfiguration.addAllowedMethod("*");
//        return corsConfiguration;  
//    }
	
	
    
    @Bean
    public Docket demoApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("yihe")
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .pathMapping("/")  // base，最终调用接口后会和paths拼接在一起
                .select()          // 选择那些路径和api会生成document
                .apis(RequestHandlerSelectors.basePackage("com.sell"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(demoApiInfo());
    }
    
    @Bean  
    public PageHelper pageHelper() {  
       System.out.println("MyBatisConfiguration.pageHelper()");  
        PageHelper pageHelper = new PageHelper();  
        Properties p = new Properties();  
        p.setProperty("offsetAsPageNum", "true");  
        p.setProperty("rowBoundsWithCount", "true");  
        p.setProperty("reasonable", "true");  
        pageHelper.setProperties(p);  
        return pageHelper;  
    } 
    
    /**
     * @MethodName: demoApiInfo
     * @Description: swagger基本信息定义
     * @return ApiInfo
     * @author: zhangfu
     * @date: 2017年6月29日
     */
    private ApiInfo demoApiInfo() {
        return new ApiInfoBuilder()
            .title("仓储物流")//大标题
            .version("1.0")//版本
            .termsOfServiceUrl("NO terms of service")
            .license("")//.license("The Apache License, Version 2.0")
            .licenseUrl("")// .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
            .build();
    }

	public static void main(String[] args) {
		SpringApplication.run(VueSell2Application.class, args);
	}
}
